#include <string>
#include <iostream>
#include <QString>
#include <ctime>
#include <vector>
#include <QtGui/QApplication>
#include <QObject>
#include <QMessageBox>

using namespace std;

#include "setupwindow.h"
#include "gameboard.h"
#include "Territory.h"
#include "Region.h"
#include "cards.h"
#include "controller.h"

Controller::Controller()
{
    //UI setup stuff
    m_gameboard = new GameBoard();
    m_setup = new SetupWindow();

    connect(m_gameboard, SIGNAL(playCreateComp()), this, SLOT(playGame()));
    m_gameboard->connect(m_setup, SIGNAL(playersCreated(vector<Player*>)), m_gameboard, SLOT(setPlayers(vector<Player*>)));
    connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(setCurClickedTer(Territory*)));
    m_gameboard->show();
    m_setup->show();

    m_turnIns = 0;
}

Controller::~Controller()
{
    delete m_gameboard;
    delete m_setup;
}

void Controller::playGame()
{
    m_curPlayerIndex = 0;  //AARON
    this->m_numAvailTerr = 39;
    m_numArmy = 15;
    this->m_gameboard->UpdateUI_Phase(QString("Territory Selection"));
    QMessageBox msg;
    msg.setText(QString("Territory Selection Phase"));
    msg.exec();
    territorySelection();
}

int Controller::getNextPlayerIndex()
{
    unsigned int curIndex = m_curPlayerIndex + 1;
    if(curIndex >= m_gameboard->getPlayers().size())
    {
        return 0;
    }
    return curIndex;
}

//Territory Selection Phase
void Controller::setCurClickedTer(Territory* clicked)
{
    if(clicked->getPlayer() != NULL)
    {
        QMessageBox msg;
        msg.setText(QString("I'm sorry, this territory has already been taken."));
        msg.exec();
        return;
    }
    m_clickedTerr = clicked;
    setPlayerTerritory();
}

void Controller::territorySelection()
{
    this->m_gameboard->UpdateUI_CurrentPlayer(m_gameboard->getPlayers().at(this->m_curPlayerIndex));
    if(this->m_numAvailTerr == 0)
    {
        disconnect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(setCurClickedTer(Territory*)));
        connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionArmyPlace(Territory*)));
        this->m_gameboard->UpdateUI_Phase(QString("Army Placement"));
        QMessageBox msg;
        msg.setText(QString("Army Placement Phase"));
        msg.exec();
        armyPlacement();
        return;
    }
}

void Controller::setPlayerTerritory()
{
    Territory* clicked = m_clickedTerr;
    Player* currentPlayer = m_gameboard->getPlayers().at(m_curPlayerIndex);

    //set Territory color
    currentPlayer->addTerritory(clicked);
    clicked->setPlayer(currentPlayer);
    m_gameboard->UpDateUI_TerritoryColor(clicked->getPlayer(), clicked);

    clicked->addStrength();
    m_gameboard->UpdateUI_TerritoryReinforcements(clicked);

    m_curPlayerIndex = getNextPlayerIndex();
    --m_numAvailTerr;
    territorySelection();
}

//Army Placement Phase
void Controller::clickActionArmyPlace(Territory* clicked)
{
    if(clicked->getPlayer() != this->m_gameboard->getPlayers().at(this->m_curPlayerIndex))
    {
        QMessageBox msg;
        msg.setText(QString("I'm sorry, this territory does not belong to you."));
        msg.exec();
        return;
    }
    m_clickedTerr = clicked;
    addArmy();
}

void Controller::armyPlacement()
{
    this->m_gameboard->UpdateUI_CurrentPlayer(m_gameboard->getPlayers().at(this->m_curPlayerIndex));
    if(m_numArmy == 0)
    {
        this->m_gameboard->UpdateUI_Phase(QString("Reinforcement"));
        disconnect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionArmyPlace(Territory*)));
        calcReinf();
        return;
    }
}

void Controller::addArmy()
{
    Territory* clicked = m_clickedTerr;
    clicked->addStrength();
    m_gameboard->UpdateUI_TerritoryReinforcements(clicked);
    if(m_curPlayerIndex == m_gameboard->getPlayers().size() - 1)
    {
        --m_numArmy;
    }
    m_curPlayerIndex = getNextPlayerIndex();
    armyPlacement();
}

//Reinforcement Phase
void Controller::calcReinf()
{
    Player* attacker = m_gameboard->getPlayers().at(m_curPlayerIndex);
    m_reinforce = 0;

    //standard reinforcements
    int reinforce = attacker->getNumTerr() / 3;

    //region reinforcements
    for(unsigned int i = 0;i < m_gameboard->getRegions().size();++i)
    {
        for(unsigned int n = 0;n < m_gameboard->getRegions()[i]->getTerritories().size();++n)
        {
            if(m_gameboard->getRegions()[i]->getTerritories()[n]->getPlayer() != attacker)
                break;
            if(n == m_gameboard->getRegions()[i]->getTerritories().size() - 1)
            {
                reinforce += m_gameboard->getRegions()[i]->getReinf();
                break;
            }
        }
    }
    //minimum reinforcements
    if(reinforce < 3)
        reinforce = 3;

    m_reinforce += reinforce;
    m_gameboard->ViewCards(attacker);
    m_done = false;
    QMessageBox msg;
    msg.setText(QString("Card Turn In"));
    msg.exec();
    msg.setText(QString("When you are finished please select Turned In."));
    msg.exec();

    connect(m_gameboard, SIGNAL(selectedCard(Cards*)), this, SLOT(clickActionCards(Cards*)));
    connect(m_gameboard, SIGNAL(turnInCardsBtn()), this, SLOT(clickActionDone()));
    m_clickedCard1 = NULL;
    m_clickedCard2 = NULL;
    m_clickedCard3 = NULL;

    cardWindow();      //Card Turn in for reinforcements

    return;
}

void Controller::clickActionCards(Cards* clicked)
{
    if(clicked == NULL)
    {
        return;
    }
    else if(m_clickedCard1 == NULL)
    {
        m_clickedCard1 = clicked;
        return;
    }
    else if(m_clickedCard2 == NULL && clicked != m_clickedCard1)
    {
        m_clickedCard2 = clicked;
        return;
    }
    else if(m_clickedCard3 == NULL && clicked != m_clickedCard1 && clicked != m_clickedCard2)
    {
        m_clickedCard3 = clicked;
        turnInCards();
        return;
    }
}

void Controller::clickActionDone()
{
    m_done = true;
    cardWindow();
    return;
}

void Controller::cardWindow()
{
    if(m_done)
    {
        disconnect(m_gameboard, SIGNAL(selectedCard(Cards*)), this, SLOT(clickActionCards(Cards*)));
        disconnect(m_gameboard, SIGNAL(turnInCardsBtn()), this, SLOT(clickActionDone()));
        m_gameboard->HideCards();
        connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionReinf(Territory*)));
        m_done = false;
        QMessageBox msg;
        msg.setText(QString("Reinforcement Phase"));
        msg.exec();
        reinforce();
        return;
    }
}

void Controller::turnInCards()
{
    Cards* One = m_clickedCard1;
    Cards* Two = m_clickedCard2;
    Cards* Three = m_clickedCard3;
    Player* attacker = m_gameboard->getPlayers().at(m_curPlayerIndex);

    m_turnInAmount = 0;
    //find out worth of turn in
    int worth;
    int TurnIn = m_turnIns + 1;
    if(TurnIn < 6)
        worth = 2 * TurnIn + 2;
    else if(TurnIn >= 6)
        worth = 15 + 5 * (TurnIn - 6);

    //find if the cards are a legitimate combination
    if(One->getType() == Two->getType() == Three->getType())
    {
        m_turnInAmount += worth;
    }
    else if((One->getType() + Two->getType() + Three->getType()) == 3)
    {
        m_turnInAmount += worth;
    }
    else if((One->getType() + Two->getType() + Three->getType()) > 6)
    {
        m_turnInAmount += worth;
    }
    else if(One->getType() == 3 || One->getType() == 1)
    {
        if(Two->getType() == 3 || Two->getType() == 1)
        {
            if(Three->getType() == 3 || One->getType() == 1)
            {
                m_turnInAmount += worth;
            }
        }
    }

    if(m_turnInAmount > 0)
    {
        //Remove Cards from Player
        for(unsigned int n = 0;n < attacker->getCards().size();++n)
        {
            if(attacker->getCards()[n] == One)
            {
                attacker->getCards().at(n) = NULL;
            }
            else if(attacker->getCards()[n] == Two)
            {
                attacker->getCards().at(n) = NULL;
            }
            else if(attacker->getCards()[n] == Three)
            {
                attacker->getCards().at(n) = NULL;
            }
        }
        //Add Cards to Deck
        m_gameboard->getDeck().push_back(One);
        m_gameboard->getDeck().push_back(Two);
        m_gameboard->getDeck().push_back(Three);

        QMessageBox msg;
        msg.setText(QString("You turn in was successful!"));
        msg.exec();

        m_gameboard->ViewCards(attacker);
        ++m_turnIns;
    }
    else
    {
        QMessageBox msg;
        msg.setText(QString("Your turn in was unsuccessful."));
        msg.exec();
    }
    m_clickedCard1 = NULL;
    m_clickedCard2 = NULL;
    m_clickedCard3 = NULL;

    m_reinforce += m_turnInAmount;
    cardWindow();
}

void Controller::clickActionReinf(Territory* clicked)
{
    if(clicked->getPlayer() != this->m_gameboard->getPlayers().at(this->m_curPlayerIndex))
    {
        QMessageBox msg;
        msg.setText(QString("I'm sorry, this territory does not belong to you."));
        msg.exec();
        return;
    }
    m_clickedTerr = clicked;
    placeReinf();
}

void Controller::reinforce()
{
    if(m_reinforce == 0)
    {
        m_prevNumTerr = m_gameboard->getPlayers().at(m_curPlayerIndex)->getNumTerr();
        m_clickedTerr = NULL;
        m_clickedTerr2 = NULL;
        disconnect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionReinf(Territory*)));
        connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionAttack(Territory*)));
        connect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext()));
        this->m_gameboard->UpdateUI_Phase(QString("Attack"));
        QMessageBox msg;
        msg.setText(QString("Attack Phase"));
        msg.exec();
        m_next = false;
        m_roll = false;
        attack();
        return;
    }
}

void Controller::placeReinf()
{
    Territory* clicked = m_clickedTerr;
    clicked->addStrength();
    m_gameboard->UpdateUI_TerritoryReinforcements(clicked);
    --m_reinforce;
    reinforce();
}

//Attack Phase
void Controller::clickActionNext()
{
    m_next = true;
    attack();
}

void Controller::clickActionAttack(Territory* clicked)
{
    if(m_clickedTerr == NULL)
    {
        if(clicked->getPlayer() != this->m_gameboard->getPlayers().at(this->m_curPlayerIndex))
        {
            QMessageBox msg;
            msg.setText(QString("You must attack from your own territory."));
            msg.exec();
            return;
        }
        m_clickedTerr = clicked;
        return;
    }
    else
    {
        if(clicked->getPlayer() == this->m_gameboard->getPlayers().at(this->m_curPlayerIndex))
        {
            m_clickedTerr = NULL;
            QMessageBox msg;
            msg.setText(QString("You cannot attack your own territory."));
            msg.exec();
            return;
        }
        else if(!clicked->isNeighbor(m_clickedTerr))
        {
            m_clickedTerr = NULL;
            QMessageBox msg;
            msg.setText(QString("You must attack a neighboring territory."));
            msg.exec();
            return;
        }
        m_clickedTerr2 = clicked;
        disconnect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionAttack(Territory*)));
        disconnect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext()));
        connect(m_gameboard, SIGNAL(rollBtn()), this, SLOT(clickActionRoll()));
        connect(m_gameboard, SIGNAL(stopRollBtn()), this, SLOT(clickActionStopRoll()));
        QMessageBox msg;
        msg.setText(QString("Select Roll to attack or select Stop to end attack."));
        msg.exec();
        attackTerr();
        return;
    }
}

int GenerateRandom(int low, int high)
{
        int k;
        double d;

        d = (double) rand() / ((double) RAND_MAX + 1);
        k = (int) (d * (high - low + 1));
        return (low + k);
}

int diceRoll()
{
        int dieVal;
        dieVal = GenerateRandom(1, 6);
        return dieVal;
}

vector<int> BubbleSort(vector<int> toSort)
{
    for(unsigned int n = 1;n < toSort.size();++n)
    {
        for(unsigned int i = 0;i + n < toSort.size();++i)
        {
            if(toSort[i] < toSort[i+1])
            {
                int temp = toSort[i];
                toSort[i] = toSort[i+1];
                toSort[i+1] = temp;
            }

        }
    }
    return toSort;
}

void Controller::attack()
{
    if(m_next)
    {
        if(m_gameboard->getPlayers().at(m_curPlayerIndex)->getNumTerr() > m_prevNumTerr)
        {
            winCard(m_gameboard->getPlayers().at(m_curPlayerIndex));
        }
        disconnect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionAttack(Territory*)));
        disconnect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext()));
        connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionTactical(Territory*)));
        connect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext2()));
        m_clickedTerr = NULL;
        m_clickedTerr2 = NULL;
        this->m_gameboard->UpdateUI_Phase(QString("Tactical Move"));
        QMessageBox msg;
        msg.setText(QString("Tactical Move Phase"));
        msg.exec();
        m_next = false;
        tacticalMove();
        return;
    }
}

void Controller::clickActionRoll()
{
    m_roll = true;
    attackTerr();
}

void Controller::clickActionStopRoll()
{
    disconnect(m_gameboard, SIGNAL(rollBtn()), this, SLOT(clickActionRoll()));
    disconnect(m_gameboard, SIGNAL(stopRollBtn()), this, SLOT(clickActionStopRoll()));
    connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionAttack(Territory*)));
    connect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext()));
    m_clickedTerr = NULL;
    m_clickedTerr2 = NULL;
    QMessageBox msg;
    msg.setText(QString("Attack ended."));
    msg.exec();
    attack();
    return;
}

void Controller::attackTerr()
{
    Territory* from = m_clickedTerr;
    Territory* to = m_clickedTerr2;

    if(m_roll)
    {
        if(from->getStrength() > 1 && to->getStrength() > 0)
        {
            rollTime();
        }
    }

    else if(from->getStrength() < 2 || to->getStrength() <= 0)
    {
        //attacker takes over territory
        if(to->getStrength() == 0)
        {
            QMessageBox msg;
            msg.setText(QString("Defender was overwhelmed."));
            msg.exec();

            Player* defender = to->getPlayer();
            for(unsigned int n = 0;n < defender->getTerritories().size();++n)
            {
                if(to == defender->getTerritories().at(n))
                {
                    defender->getTerritories().at(n) = NULL;
                }
            }
            Player* attacker = m_gameboard->getPlayers().at(m_curPlayerIndex);
            to->setPlayer(attacker);
            attacker->addTerritory(to);
            m_gameboard->UpDateUI_TerritoryColor(to->getPlayer(), to);

            //Loser
            if(defender->getNumTerr() == 0)
            {
                //gives attacker losers cards
                for(unsigned int n = 0;n < defender->getCards().size();++n)
                {
                    attacker->addCard(defender->getCards()[n]);
                    defender->getCards().erase(defender->getCards().begin() + n);
                }
                //remove loser from lineup
                for(unsigned int n = 0;n < m_gameboard->getPlayers().size();++n)
                {
                    if(m_gameboard->getPlayers()[n] == defender)
                        m_gameboard->getPlayers().erase(m_gameboard->getPlayers().begin() + n);
                }
            }

            //choose amount to move to new territory
            m_movedArmies = 0;
            connect(m_gameboard, SIGNAL(numArmiesChosen()), this, SLOT(clickActionMoveArmies()));
            m_gameboard->connect(this, SIGNAL(setSlider(Territory*)), m_gameboard, SLOT(setSliderNum(Territory*)));
            emit setSlider(from);
            chooseArmies();
            return;
        }
        else
        {
            disconnect(m_gameboard, SIGNAL(rollBtn()), this, SLOT(clickActionRoll()));
            disconnect(m_gameboard, SIGNAL(stopRollBtn()), this, SLOT(clickActionStopRoll()));
            connect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext()));
            connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionAttack(Territory*)));

            m_clickedTerr = NULL;
            m_clickedTerr2 = NULL;
            m_roll = false;

            QMessageBox msg;
            msg.setText(QString("Attacker was turned back."));
            msg.exec();
            attack();
            return;
        }

    }
    return;
}

void Controller::clickActionMoveArmies()
{
    m_movedArmies = m_gameboard->movingArmies();
    chooseArmies();
    return;
}

void Controller::chooseArmies()
{
    if(0 < m_movedArmies)
    {
        disconnect(m_gameboard, SIGNAL(numArmiesChosen()), this, SLOT(clickActionMoveArmies()));

        Territory* from = m_clickedTerr;
        Territory* to = m_clickedTerr2;
        Player* attacker = from->getPlayer();

        to->setStrength(m_movedArmies);
        m_gameboard->UpdateUI_TerritoryReinforcements(to);
        from->setStrength(from->getStrength() - m_movedArmies);
        m_gameboard->UpdateUI_TerritoryReinforcements(from);

        if(attacker->getCards().size() >= 5)
        {
            cardWindow();
            reinforce();
        }

        this->m_gameboard->UpdateUI_CurrentPlayer(m_gameboard->getPlayers().at(this->m_curPlayerIndex));
        disconnect(m_gameboard, SIGNAL(rollBtn()), this, SLOT(clickActionRoll()));
        disconnect(m_gameboard, SIGNAL(stopRollBtn()), this, SLOT(clickActionStopRoll()));
        connect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext()));
        connect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionAttack(Territory*)));

        m_clickedTerr = NULL;
        m_clickedTerr2 = NULL;
        m_roll = false;
        m_movedArmies = 0;

        QMessageBox msg;
        msg.setText(QString("Your armies have been moved."));
        msg.exec();

        attack();
        return;
    }
}

void Controller::rollTime()
{
    srand ((unsigned) time (0));

    Territory* from = m_clickedTerr;
    Territory* to = m_clickedTerr2;

    int attack = from->getStrength() - 1;
    int defend = to->getStrength();

    vector<int> attackDice;
    vector<int> defendDice;

    //CHECK
    int attack1 = -1;
    int attack2 = -1;
    int attack3 = -1;
    int defend1 = -1;
    int defend2 = -1;

    //Rolls and sorts dice
    attack1 = diceRoll();
    attackDice.push_back(attack1);
    defend1 = diceRoll();
    defendDice.push_back(defend1);
    if(attack > 1)
    {
        attack2 = diceRoll();
        attackDice.push_back(attack2);
    }
    if(defend > 1)
    {
        defend2 = diceRoll();
        defendDice.push_back(defend2);
    }
    if(attack > 2)
    {
        attack3 = diceRoll();
        attackDice.push_back(attack3);
    }

    attackDice = BubbleSort(attackDice);

    //create a for loop to send signal to gamboard
    for(unsigned int i = 0; i < attackDice.size(); ++i)
    {
        if(i == 0)
        {
            m_gameboard->UpdateUI_Dice1(attackDice[i]);
        }
        else if(i == 1)
        {
            m_gameboard->UpdateUI_Dice2(attackDice[i]);
        }
        else if(i == 2)
        {
            m_gameboard->UpdateUI_Dice3(attackDice[i]);
        }
    }
    if(attackDice.size() == 1)
    {
        m_gameboard->UpdateUI_Dice2(-1);
        m_gameboard->UpdateUI_Dice3(-1);
    }
    if(attackDice.size() == 2)
    {
        m_gameboard->UpdateUI_Dice3(-1);
    }

    defendDice = BubbleSort(defendDice);

    //create a for loop to send signal to gameboard
    for(unsigned int i = 0; i < defendDice.size(); ++i)
    {
        if(i == 0)
        {
           m_gameboard->UpdateUI_Dice4(defendDice[i]);
        }
        else if(i == 1)
        {
           m_gameboard->UpdateUI_Dice5(defendDice[i]);
        }
    }
    if(defendDice.size() == 1)
    {
        m_gameboard->UpdateUI_Dice5(-1);
    }

    //compares dice
    if(attackDice[0] > defendDice[0])
    {
        to->minusStrength();
        m_gameboard->UpdateUI_TerritoryReinforcements(to);
    }
    else
    {
        from->minusStrength();
        m_gameboard->UpdateUI_TerritoryReinforcements(from);
    }
    if(attack > 1 && defend > 1)
    {
        if(attackDice[1] > defendDice[1])
        {
            to->minusStrength();
            m_gameboard->UpdateUI_TerritoryReinforcements(to);
        }
        else
        {
            from->minusStrength();
            m_gameboard->UpdateUI_TerritoryReinforcements(from);
        }
    }
    m_roll = false;
    attackTerr();
    return;
}

void Controller::winCard(Player* attacker)
{
    int random = GenerateRandom(0,m_gameboard->getDeck().size());
    if(m_gameboard->getDeck().at(random - 1) == NULL)
    {
        winCard(m_gameboard->getPlayers().at(m_curPlayerIndex));
        return;
    }
    Cards* NewCard = m_gameboard->getDeck().at(random - 1);
    m_gameboard->getDeck().at(random - 1) = NULL;
    attacker->addCard(NewCard);
    return;
}

//Tactical Move Phase
void Controller::clickActionTactical(Territory* clicked)
{
    if(m_clickedTerr2 != NULL)
    {
        m_clickedTerr = NULL;
        m_clickedTerr2 = NULL;
        disconnect(m_gameboard, SIGNAL(numArmiesChosen()), this, SLOT(clickActionMoveArmies2()));
    }
    if(m_clickedTerr == NULL)
    {
        if(clicked->getPlayer() != this->m_gameboard->getPlayers().at(this->m_curPlayerIndex))
        {
            QMessageBox msg;
            msg.setText(QString("You cannot choose another's territory."));
            msg.exec();
            return;
        }
        m_clickedTerr = clicked;
        return;
    }
    else
    {
        if(clicked->getPlayer() != this->m_gameboard->getPlayers().at(this->m_curPlayerIndex))
        {
            m_clickedTerr = NULL;
            QMessageBox msg;
            msg.setText(QString("You cannot choose another's territory."));
            msg.exec();
            return;
        }
        else if(!clicked->isNeighbor(m_clickedTerr))
        {
            m_clickedTerr = NULL;
            QMessageBox msg;
            msg.setText(QString("You must reinforce a neighboring territory."));
            msg.exec();
            return;
        }
        m_clickedTerr2 = clicked;
        connect(m_gameboard, SIGNAL(numArmiesChosen()), this, SLOT(clickActionMoveArmies2()));
        QMessageBox msg;
        msg.setText(QString("Slide the bar to move the number of armies you wish."));
        msg.exec();
        emit setSlider(m_clickedTerr);
        tacticalMove();
        return;
    }
}

void Controller::clickActionNext2()
{
    m_next = true;
    tacticalMove();
}

void Controller::tacticalMove()
{
    if(m_next)
    {
        if(m_gameboard->getPlayers().at(m_curPlayerIndex)->getNumTerr() == 40)
        {
            QMessageBox msg;
            msg.setText(QString("You control all of Westmont!"));
            msg.exec();
            return;
        }
        newTurn();
    }
}

void Controller::clickActionMoveArmies2()
{
    m_movedArmies = m_gameboard->movingArmies();
    if(0 < m_movedArmies)
    {
        disconnect(m_gameboard, SIGNAL(numArmiesChosen()), this, SLOT(clickActionMoveArmies2()));
        Territory* click1 = m_clickedTerr;
        Territory* click2 = m_clickedTerr2;
        click2->setStrength(click2->getStrength() + m_movedArmies);
        m_gameboard->UpdateUI_TerritoryReinforcements(click2);
        click1->setStrength(click1->getStrength() - m_movedArmies);
        m_gameboard->UpdateUI_TerritoryReinforcements(click1);
        m_movedArmies = 0;
        m_next = true;
        tacticalMove();
    }
    return;
}

//Recycle
void Controller::newTurn()
{
    m_next = false;
    m_curPlayerIndex = getNextPlayerIndex();
    m_gameboard->UpdateUI_CurrentPlayer(m_gameboard->getPlayers().at(this->m_curPlayerIndex));
    this->m_gameboard->UpdateUI_Phase(QString("Reinforcement"));
    disconnect(m_gameboard, SIGNAL(nextBtn()), this, SLOT(clickActionNext2()));
    disconnect(m_gameboard, SIGNAL(selectedTerritory(Territory*)), this, SLOT(clickActionTactical(Territory*)));
    connect(m_gameboard, SIGNAL(selectedCard(Cards*)), this, SLOT(clickActionCards(Cards*)));
    calcReinf();
}
